package android.main;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.bbs.Bbs;
import android.content.Intent;
import android.manage.Management;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
import com.example.piano_application.R;

/**
 * 메인화면
 * - 이미지 버튼별 처리
 * */

@SuppressLint("HandlerLeak")
public class Main_menu extends Activity implements OnClickListener{
	
//	private String id;
	private boolean backkey = false;
	private int mValue;
	
	private ImageButton img_manage;
	private ImageButton img_call;	
	private ImageButton img_send;
	private ImageButton img_bbs;
	
//	public Main_menu() {
//        Utility.makeFolder();
//    }
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_2x);
//        Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
//        int width = display.getWidth();
//        int height = display.getHeight();
//        Toast.makeText(this, "width : "+width +"height : "+height, Toast.LENGTH_LONG).show();
        
//        Intent intent = getIntent();
//        id = intent.getStringExtra("id");
        
        img_manage = (ImageButton)findViewById(R.id.img_manage_2x);
        img_manage.setOnClickListener(this);
        
        img_call = (ImageButton)findViewById(R.id.img_call_2x);
        img_call.setOnClickListener(this);
        
        img_send = (ImageButton)findViewById(R.id.img_send_2x);
        img_send.setOnClickListener(this);
        
        img_bbs = (ImageButton)findViewById(R.id.img_bbs_2x);
        img_bbs.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v.getId() == R.id.img_manage_2x) {
			Intent intent = null;
			intent = new Intent(getApplicationContext(), Management.class);
			startActivity(intent);
        } else if (v.getId() == R.id.img_call_2x) {
        	String telNumber = "tel:" + "010-9037-1961";
        	Intent intent = new Intent (Intent.ACTION_DIAL,Uri.parse(telNumber));
        	//Intent intent = new Intent (Intent.ACTION_CALL,Uri.parse(telNumber));
        	intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        	startActivity(intent);
        } else if (v.getId() == R.id.img_send_2x) {
        	
        } else if (v.getId() == R.id.img_bbs_2x) {
        	Intent intent = null;
			intent = new Intent(getApplicationContext(), Bbs.class);
			startActivity(intent);
        } 
	}

	
	/** 뒤로가기 */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && backkey == true) {
            finish();
        } else if (keyCode == KeyEvent.KEYCODE_BACK) {
            Toast.makeText(getApplicationContext(), "'뒤로가기'를 한번 더 누르면 종료됩니다.", Toast.LENGTH_SHORT).show();
            backkey = true;
            mValue = 1;
        } 
        return false;
    }

    /** 뒤로가기 이벤트 헨들러 */
    Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                mValue = 0;
                backkey = false;
            }
        }
    };
    
    /** 뒤로가기 이벤트 스레드 */
    class BackThread extends Thread {
        BackThread(Handler handler) {
            mHandler = handler;
        }
        
        public void run() {
            while(true) {
                try { Thread.sleep(2000); } catch (InterruptedException e) {;}
                Message msg = Message.obtain(mHandler, mValue, 0, 0);
                mHandler.sendMessage(msg);
            }
        }
    }
	
			
}
